<template>
    <div class="pure-g">
    </div>
    <div class="pure-g content">
        <div class="pure-form pure-u-1">
            <h4>请登录您的账号</h4>
            <fieldset class="pure-group">
                <input type="text" class="pure-input-1" placeholder="用户名" id="UL_Name" v-model="UL_Name" required autofocus>
                <input type="password" class="pure-input-1" placeholder="密　码" id="UL_Pwd" v-model="UL_Pwd" required>
            </fieldset>
            <button class="pure-button pure-u-1 pure-button-primary" @click="SaveData">登　录</button>
        </div>
    </div>
    <div class="pure-g copyright">
        <div class="pure-u-1"> 2023 ZS. </div>
    </div>
</template>

<script setup lang="ts">
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
import axios from 'axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../store/index.ts'



const UL_Name = ref('');
const UL_Pwd = ref('');

//创建仓库实例
const userStore = useUserStore();

//登录函数
const SaveData = () => {
    //判断用户名不能为空
    if (!UL_Name.value) {
        alert('用户名不能为空！');
        return;
    }

    //判断密码不能为空
    if (!UL_Pwd.value) {
        alert('密码不能为空！');
        return;
    }

    //https://localhost:44305//api/Users/GetUserLogin?name=admin&pwd=123456
    axios.post('https://localhost:44305/api/Users/GetUserLogin?name=' + UL_Name.value + '&pwd=' + UL_Pwd.value)
        .then(res => {
            if (res.data != null) {
                alert('登录成功');
                SercahInfo(res.data.U_Id);
                //TakeRegister(res.data.U_Id);
            }
            else {
                console.log('登录失败');
            }
        })
        .catch(error => {
            console.log(error);
        })
}


//通过路由跳转用户详细页面
const router = useRouter();
const TakeRegister = (UID: string) => {
    router.push({
        name: "UserRegister",
        params: {
            Uid: UID
        }
    })
}


const SercahInfo = (Uid: string) => {
    axios.get('https://localhost:44305/api/Users/SearchUserInfo?guid=' + Uid)
        .then(res => {
            if (res.data === 0) {
                TakeRegister(Uid);
            }
            else {
                //将用户编号保存到仓库中
                userStore.UserLoginId = Uid;

                alert(userStore.UserLoginId);
                router.push({
                    name: "Home"
                })
            }
        })
        .catch(error => {
            console.log(error);
        })
}

</script>





